package com.baidu.littlepainter.fragments.login

import android.view.View
import android.widget.Toast
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsAnimationCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.baidu.littlepainter.R
import com.baidu.littlepainter.databinding.FragmentPinLoginBinding
import com.baidu.littlepainter.fragments.base.BaseFragment
import com.baidu.littlepainter.utils.KeyboardAnimation
import com.baidu.littlepainter.utils.LoginRegisterResult
import com.baidu.littlepainter.utils.PasswordType
import com.baidu.littlepainter.utils.ViewUtils
import com.baidu.littlepainter.utils.WrongType
import com.baidu.littlepainter.viewmodel.MainViewModel

class PinLoginFragment: BaseFragment<FragmentPinLoginBinding>() {
    private val viewModel: MainViewModel by activityViewModels()
    override fun initBinding(): FragmentPinLoginBinding {
        return FragmentPinLoginBinding.inflate(layoutInflater)
    }

    override fun initView() {
        viewModel.setToLoginFragment()

        mBinding.usernameView.addTextChangeListener {
            changeLoginButtonState()
        }
        mBinding.passweordView.addTextChangeListener {
            changeLoginButtonState()
        }
        mBinding.loginButton.setOnClickListener {
            //使用PIN登录
            viewModel.login(
                mBinding.usernameView.text().trim(),
                mBinding.passweordView.text().trim(),
                PasswordType.PIN
            )
        }

        //监听登录结果
        viewModel.loginRegisterResult.observe(viewLifecycleOwner){
            if (it is LoginRegisterResult.Success){
                //登录成功进入主界面
            }else if (it is LoginRegisterResult.Failure){
                //登录失败
                if (it.type == WrongType.USER_NOT_FOUND){
                    mBinding.usernameView.showError()
                }
                if (it.type == WrongType.WRONG_PASSWORD){
                    mBinding.passweordView.showError()
                }
            }
        }

        //监听用户数据
        viewModel.users.observe(viewLifecycleOwner){
            if (it.isEmpty()){ //没有用户就隐藏
                mBinding.switchTextView.visibility = View.INVISIBLE
            }else{ //有用户才显示
                mBinding.switchTextView.visibility = View.VISIBLE
            }
        }

        //监听注册事件
        mBinding.registerTextView.setOnClickListener {
            findNavController().navigate(R.id.action_pinLoginFragment_to_pinRegisterFragment)
        }

        //切换为图案解锁
        mBinding.switchTextView.setOnClickListener {
            findNavController().navigate(R.id.action_pinLoginFragment_to_picLoginFragment)
        }
    }

    override fun initKeyboardAnimationView(): View? {
        return mBinding.inputlayout
    }

    //修改登录按钮是否可以点击的状态
    private fun changeLoginButtonState() {
        mBinding.loginButton.isEnabled =
            mBinding.usernameView.text().isNotEmpty()
                && mBinding.passweordView.text().isNotEmpty()
    }


}